home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / compat / strtol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  1.9 KB  |  84 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * strtol.c --
  3.  *
  4.  *    Source code for the "strtol" library procedure.
  5.  *
  6.  * Copyright (c) 1988 The Regents of the University of California.
  7.  * Copyright (c) 1994 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) strtol.c 1.4 96/02/15 12:08:23
  13.  */
  14.  
  15. #include <ctype.h>
  16.  
  17.  
  18. /*
  19.  *----------------------------------------------------------------------
  20.  *
  21.  * strtol --
  22.  *
  23.  *    Convert an ASCII string into an integer.
  24.  *
  25.  * Results:
  26.  *    The return value is the integer equivalent of string.  If endPtr
  27.  *    is non-NULL, then *endPtr is filled in with the character
  28.  *    after the last one that was part of the integer.  If string
  29.  *    doesn't contain a valid integer value, then zero is returned
  30.  *    and *endPtr is set to string.
  31.  *
  32.  * Side effects:
  33.  *    None.
  34.  *
  35.  *----------------------------------------------------------------------
  36.  */
  37.  
  38. long int
  39. strtol(string, endPtr, base)
  40.     char *string;        /* String of ASCII digits, possibly
  41.                  * preceded by white space.  For bases
  42.                  * greater than 10, either lower- or
  43.                  * upper-case digits may be used.
  44.                  */
  45.     char **endPtr;        /* Where to store address of terminating
  46.                  * character, or NULL. */
  47.     int base;            /* Base for conversion.  Must be less
  48.                  * than 37.  If 0, then the base is chosen
  49.                  * from the leading characters of string:
  50.                  * "0x" means hex, "0" means octal, anything
  51.                  * else means decimal.
  52.                  */
  53. {
  54.     register char *p;
  55.     int result;
  56.  
  57.     /*
  58.      * Skip any leading blanks.
  59.      */
  60.  
  61.     p = string;
  62.     while (isspace(*p)) {
  63.     p += 1;
  64.     }
  65.  
  66.     /*
  67.      * Check for a sign.
  68.      */
  69.  
  70.     if (*p == '-') {
  71.     p += 1;
  72.     result = -(strtoul(p, endPtr, base));
  73.     } else {
  74.     if (*p == '+') {
  75.         p += 1;
  76.     }
  77.     result = strtoul(p, endPtr, base);
  78.     }
  79.     if ((result == 0) && (endPtr != 0) && (*endPtr == p)) {
  80.     *endPtr = string;
  81.     }
  82.     return result;
  83. }
  84.